import { ObjectDirective } from "vue"
import { useAuthStore } from "@/store/modules/auth"
import { isArray, isString } from "@/utils"

export const permissions: ObjectDirective = {
    mounted(el: HTMLButtonElement, binding) {
        const perms = binding.value
        if (perms == undefined) {
            return
        }
        const authStore = useAuthStore()
        let hasPermission = false
        if (isArray(perms)) {
            hasPermission = authStore.hasPermissions(perms)
        } else if (isString(perms)) {
            hasPermission = authStore.hasPermission(perms)
        }
        if (!hasPermission) {
            el.remove()
        }
    }
}
